package cn.yuhe.org.service.impl;


import cn.yuhe.basic.service.ILoginService;
import cn.yuhe.basic.service.impl.BaseServiceImpl;
import cn.yuhe.basic.utils.MD5Utils;
import cn.yuhe.basic.utils.PageList;
import cn.yuhe.basic.utils.StrUtils;
import cn.yuhe.org.domain.Employee;
import cn.yuhe.org.mapper.EmployeeMapper;
import cn.yuhe.org.query.EmployeeQuery;
import cn.yuhe.org.service.IEmployeeService;
import cn.yuhe.org.service.IShopService;
import cn.yuhe.user.domain.Logininfo;
import cn.yuhe.user.mapper.LogininfoMapper;
import cn.yuhe.user.service.ILogininfoService;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @Description: 部门业务层实现类
* @Author: Mr.She
* @Version: 1.0
* @Date:  2022/6/21 16:42
*/
@Service
// 只读事务
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ILogininfoService logininfoService;

    @Autowired
    private IShopService shopService;

    @Override
    public void save(Employee employee) {
        // 1,初始化Employee，并保存
        initEmployee(employee);
        employeeMapper.save(employee);
        // 添加数据到中间表
        employeeMapper.saveEmployeeRole(employee.getId(),employee.getRoles());
        // 2,初始化logininfo，并保存
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoService.save(logininfo);
        // 3,将logininfo的主键设置到Employee外键
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.update(employee);
    }



    @Override
    public void update(Employee employee) {
        // 1,初始化Employee
        initEmployee(employee);
        employeeMapper.update(employee);
        // 删除中间表对应id数据
        employeeMapper.remove2(employee.getId());
        employeeMapper.saveEmployeeRole(employee.getId(),employee.getRoles());

        // 2,初始化logininfo，并保存
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoService.update(logininfo);
    }

    @Override
    public void remove(Long id) {
        employeeMapper.remove2(id);
        Long logininfoId = employeeMapper.loadById(id).getLogininfoId();
        Long shopId = employeeMapper.loadById(id).getShopId();
        logininfoService.remove(logininfoId);
        shopService.remove(shopId);
        employeeMapper.remove(id);

    }




    // 初始化Employee方法
    private void initEmployee(Employee employee) {
        // 获取盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 设置默认密码
        String saltPassword = MD5Utils.encrypByMd5(salt + employee.getPassword());
        // 设置Employee盐值
        employee.setSalt(salt);
        // 设置Employee加盐密码
        employee.setPassword(saltPassword);
    }

    // 2,初始化logininfo方法
    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        return logininfo;
    }

    @Override
    public Employee loadByUserName(String username) {
        return employeeMapper.loadByUserName(username);
    }
}
